<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是makefile——条件判断和函数相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-05-Makefile-07-条件判断和函数">
<meta property="og:url" content="https://sumumm.github.io/post/8aa811b0.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是makefile——条件判断和函数相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-23T05:58:24.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.990Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV02-编译基础">
<meta property="article:tag" content="makefile">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://sumumm.github.io/post/8aa811b0.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/8aa811b0.html","path":"post/8aa811b0.html","title":"LV02-05-Makefile-07-条件判断和函数"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-05-Makefile-07-条件判断和函数 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD"><span class="nav-text">一、条件判断</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E6%A0%BC%E5%BC%8F"><span class="nav-text">1.条件判断格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-ifeq"><span class="nav-text">2. ifeq </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-ifneq"><span class="nav-text">3. ifneq </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-ifdef"><span class="nav-text">4. ifdef </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-ifndef"><span class="nav-text">5. ifndef </span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81-Makefile-%E5%87%BD%E6%95%B0"><span class="nav-text">二、 Makefile 函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="nav-text">1.函数的调用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0"><span class="nav-text">2.字符串处理函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0"><span class="nav-text">2.1字符串替换函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1-subst"><span class="nav-text">2.1.1 subst </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.1.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E6%A8%A1%E5%BC%8F%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0"><span class="nav-text">2.2模式字符串替换函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-patsubst"><span class="nav-text">2.2.1 patsubst </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E5%8E%BB%E7%A9%BA%E6%A0%BC%E5%87%BD%E6%95%B0"><span class="nav-text">2.3去空格函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1-strip"><span class="nav-text">2.3.1 strip </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.3.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4%E6%9F%A5%E6%89%BE%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%87%BD%E6%95%B0"><span class="nav-text">2.4查找字符串函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-1-findstring"><span class="nav-text">2.4.1 findstring </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.4.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5%E8%BF%87%E6%BB%A4%E5%87%BD%E6%95%B0"><span class="nav-text">2.5过滤函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1-filter"><span class="nav-text">2.5.1 filter </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.5.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6%E5%8F%8D%E8%BF%87%E6%BB%A4%E5%87%BD%E6%95%B0"><span class="nav-text">2.6反过滤函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-1-filter-out"><span class="nav-text">2.6.1 filter-out </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.6.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7%E6%8E%92%E5%BA%8F%E5%87%BD%E6%95%B0"><span class="nav-text">2.7排序函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-1-sort"><span class="nav-text">2.7.1 sort </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.7.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8%E5%8F%96%E5%8D%95%E8%AF%8D%E5%87%BD%E6%95%B0"><span class="nav-text">2.8取单词函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8-1-word"><span class="nav-text">2.8.1 word </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.8.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-9%E5%8D%95%E8%AF%8D%E4%B8%AA%E6%95%B0%E7%BB%9F%E8%AE%A1%E5%87%BD%E6%95%B0"><span class="nav-text">2.9单词个数统计函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-9-1-words"><span class="nav-text">2.9.1 words </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-9-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.9.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-10%E9%A6%96%E5%8D%95%E8%AF%8D%E5%87%BD%E6%95%B0"><span class="nav-text">2.10首单词函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-1-firstword"><span class="nav-text">2.10.1 firstword </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.10.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-11%E7%BB%BC%E5%90%88%E5%BA%94%E7%94%A8"><span class="nav-text">2.11综合应用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%96%87%E4%BB%B6%E5%90%8D%E6%93%8D%E4%BD%9C%E5%87%BD%E6%95%B0"><span class="nav-text">3.文件名操作函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%8F%96%E7%9B%AE%E5%BD%95%E5%87%BD%E6%95%B0"><span class="nav-text">3.1取目录函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-dir"><span class="nav-text">3.1.1 dir </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.1.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E5%8F%96%E6%96%87%E4%BB%B6%E5%87%BD%E6%95%B0"><span class="nav-text">3.2取文件函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-notdir"><span class="nav-text">3.2.1 notdir </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.2.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E5%8F%96%E5%90%8E%E7%BC%80%E5%87%BD%E6%95%B0"><span class="nav-text">3.3取后缀函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-1-suffix"><span class="nav-text">3.3.1 suffix </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.3.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4%E5%8F%96%E5%89%8D%E7%BC%80%E5%87%BD%E6%95%B0"><span class="nav-text">3.4取前缀函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-1-basename"><span class="nav-text">3.4.1 basename </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.4.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5%E5%8A%A0%E5%90%8E%E7%BC%80%E5%87%BD%E6%95%B0"><span class="nav-text">3.5加后缀函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-1-addsuffix"><span class="nav-text">3.5.1 addsuffix </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.5.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6%E5%8A%A0%E5%89%8D%E7%BC%80%E5%87%BD%E6%95%B0"><span class="nav-text">3.6加前缀函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-1-addprefix"><span class="nav-text">3.6.1 addprefix </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.6.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7%E8%BF%9E%E6%8E%A5%E5%87%BD%E6%95%B0"><span class="nav-text">3.7连接函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-7-1-join"><span class="nav-text">3.7.1 join </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-7-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.7.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-8%E8%8E%B7%E5%8F%96%E5%8C%B9%E9%85%8D%E6%A8%A1%E5%BC%8F%E6%96%87%E4%BB%B6%E5%90%8D%E5%87%BD%E6%95%B0"><span class="nav-text">3.8获取匹配模式文件名函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-8-1-wildcard"><span class="nav-text">3.8.1 wildcard </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-8-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.8.2使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%85%B6%E5%AE%83%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="nav-text">4.其它常用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-foreach-%E5%87%BD%E6%95%B0"><span class="nav-text">4.1 foreach  函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-1-foreach"><span class="nav-text">4.1.1 foreach </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-1%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.1.1使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-if-%E5%87%BD%E6%95%B0"><span class="nav-text">4.2 if 函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-if"><span class="nav-text">4.2.1 if </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-call-%E5%87%BD%E6%95%B0"><span class="nav-text">4.3 call 函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-1-call"><span class="nav-text">4.3.1 call </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.3.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-origin-%E5%87%BD%E6%95%B0"><span class="nav-text">4.4 origin 函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-4-1-origin"><span class="nav-text">4.4.1 origin </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-4-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.4.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5-shell-%E5%87%BD%E6%95%B0"><span class="nav-text">4.5 shell 函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-5-1-shell"><span class="nav-text">4.5.1 shell </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-5-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.5.2使用实例</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81-make-%E6%8E%A7%E5%88%B6%E5%87%BD%E6%95%B0"><span class="nav-text">三、 make 控制函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-error-%E5%87%BD%E6%95%B0"><span class="nav-text">1. error 函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-error"><span class="nav-text">1.1 error </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-warning-%E5%87%BD%E6%95%B0"><span class="nav-text">2. warning 函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-warning"><span class="nav-text">2.1 warning </span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81-Makefile-%E4%B8%AD%E7%9A%84-for-%E5%BE%AA%E7%8E%AF"><span class="nav-text">四、 Makefile 中的 for 循环</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-for-%E6%A0%BC%E5%BC%8F"><span class="nav-text">1. for 格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.使用实例</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/8aa811b0.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-05-Makefile-07-条件判断和函数 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-05-Makefile-07-条件判断和函数
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-23 13:58:24" itemprop="dateCreated datePublished" datetime="2022-03-23T13:58:24+08:00">2022-03-23</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV02-编译基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>26 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是makefile——条件判断和函数相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-05-Makefile-07-%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E5%92%8C%E5%87%BD%E6%95%B0/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left"> Makefile </td><td align="left"><a href="" target="_blank"> 跟我一起写Makefile <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、条件判断"><a href="#一、条件判断" class="headerlink" title="一、条件判断"></a><font size=3>一、条件判断</font></h1><h2 id="1-条件判断格式"><a href="#1-条件判断格式" class="headerlink" title="1.条件判断格式"></a><font size=3>1.条件判断格式</font></h2><p>Makefile  编译文件时，可能会遇到需要分条件执行的情况，比如在一个工程文件中，可编译的源文件很多，但是它们的类型可能是不相同的，所以编译文件使用的编译器也可能是不同的。这个时候就用到了条件判断。</p>
<p>条件表达式语法如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">1.语法格式一</span></span><br><span class="line">&lt;conditional-directive&gt;</span><br><span class="line">	&lt;text-if-true&gt;</span><br><span class="line">endif</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">2.语法格式二</span></span><br><span class="line">&lt;conditional-directive&gt;</span><br><span class="line">	&lt;text-if-true&gt;</span><br><span class="line">else</span><br><span class="line">	&lt;text-if-false&gt;</span><br><span class="line">endif</span><br></pre></td></tr></table></figure>

<p>&lt;conditional-directive&gt; 表示的是条件关键字。在这一行上，多余的空格是被允许的，但是不能以 Tab 键做为开始（不 然就被认为是命令）。注释符 # 同样也是安全的。 else 和 endif 也一样，只要不 是以 Tab 键开始就可以。</p>
<table>
    <tr><td align="center" width=100px>关键字</td><td align="center">功能</td></tr>
    <tr><td align="center" width=100px>ifeq</td><td align="left">判断参数是否相等，相等为 true，不相等为 false。</td></tr>
    <tr><td align="center" width=100px>ifneq</td><td align="left">判断参数是否不相等，不相等为 true，相等为 false。</td></tr>
    <tr><td align="center" width=100px>ifdef</td><td align="left">判断是否有值，有值为 true，没有值为 false。</td></tr>
    <tr><td align="center" width=100px>ifndef</td><td align="left">判断是否有值，没有值为 true，有值为 false。</td></tr>
</table>

<p>【注意】</p>
<p>（1）在  make  读取  Makefile  文件时就会计算表达式的值，并根据表达式的值决定判断语句中的哪一个部分作为此  Makefile  所要执行的内容。因此在<strong>条件表达式中不能使用自动化变量</strong>，自动化变量在规则命令执行时才有效。</p>
<p>（2）为了避免混乱， make  不允许把整个条件语句分成两部分放在不同的  Makefile  的文件中。</p>
<h2 id="2-ifeq"><a href="#2-ifeq" class="headerlink" title="2. ifeq "></a><font size=3>2. ifeq </font></h2><ul>
<li>ifeq 判断参数是否相同，相同为 true</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ifeq</span> (ARG1, ARG2)</span><br><span class="line"><span class="keyword">ifeq</span> &#x27;ARG1&#x27; &#x27;ARG2&#x27;</span><br><span class="line"><span class="keyword">ifeq</span> <span class="string">&quot;ARG1&quot;</span> <span class="string">&quot;ARG2&quot;</span></span><br><span class="line"><span class="keyword">ifeq</span> <span class="string">&quot;ARG1&quot;</span> &#x27;ARG2&#x27;</span><br><span class="line"><span class="keyword">ifeq</span> &#x27;ARG1&#x27; <span class="string">&quot;ARG2&quot;</span></span><br></pre></td></tr></table></figure>

<p>比较参数 ARG1 和 ARG2 的值是否相同，相同为 true ，不相同为 false ，例如</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">CC = a</span><br><span class="line"><span class="section">test:</span></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(CC)</span>,gcc)</span><br><span class="line">	@echo <span class="string">&quot;True&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	@echo <span class="string">&quot;False&quot;</span></span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf *.o main</span><br></pre></td></tr></table></figure>

<p>在终端执行 make test ，会看到输出了以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">False</span><br></pre></td></tr></table></figure>

<h2 id="3-ifneq"><a href="#3-ifneq" class="headerlink" title="3. ifneq "></a><font size=3>3. ifneq </font></h2><ul>
<li>ifneq 判断参数是否不相等，不相等为 true</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ifneq</span> (ARG1, ARG2)</span><br><span class="line"><span class="keyword">ifneq</span> &#x27;ARG1&#x27; &#x27;ARG2&#x27;</span><br><span class="line"><span class="keyword">ifneq</span> <span class="string">&quot;ARG1&quot;</span> <span class="string">&quot;ARG2&quot;</span></span><br><span class="line"><span class="keyword">ifneq</span> <span class="string">&quot;ARG1&quot;</span> &#x27;ARG2&#x27;</span><br><span class="line"><span class="keyword">ifneq</span> &#x27;ARG1&#x27; <span class="string">&quot;ARG2&quot;</span></span><br></pre></td></tr></table></figure>

<p>比较参数 ARG1 和 ARG2 的值是否不相同，不相同为 true ，相同为 false ，例如</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">CC = a</span><br><span class="line"><span class="section">test:</span></span><br><span class="line"><span class="keyword">ifneq</span> (<span class="variable">$(CC)</span>,gcc)</span><br><span class="line">	@echo <span class="string">&quot;True&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	@echo <span class="string">&quot;False&quot;</span></span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf *.o main</span><br></pre></td></tr></table></figure>

<p>在终端执行 make test ，会看到输出了以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">True</span><br></pre></td></tr></table></figure>

<h2 id="4-ifdef"><a href="#4-ifdef" class="headerlink" title="4. ifdef "></a><font size=3>4. ifdef </font></h2><ul>
<li>ifdef 判断参数是否非空，非空值为 true</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ifdef</span> VARIABLE_NAME</span><br></pre></td></tr></table></figure>

<p>判断参数 VARIABLE_NAME 是否非空值，非空值表达式为 true ，若为空值则表达式为 false ，例如</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">a:=</span><br><span class="line"><span class="section">test:</span></span><br><span class="line"><span class="keyword">ifdef</span> a</span><br><span class="line">	@echo <span class="string">&quot;True&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	@echo <span class="string">&quot;False&quot;</span></span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf *.o main</span><br></pre></td></tr></table></figure>

<p>在终端执行 make test ，会看到输出了以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">False</span><br></pre></td></tr></table></figure>

<h2 id="5-ifndef"><a href="#5-ifndef" class="headerlink" title="5. ifndef "></a><font size=3>5. ifndef </font></h2><ul>
<li>ifndef 判断参数是否是非空值，空值为 true</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ifndef</span> VARIABLE_NAME</span><br></pre></td></tr></table></figure>

<p>判断参数 VARIABLE_NAME 是否为空值，若为空值表达式为 true ，非空值的话表达式为 false 。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">a:=</span><br><span class="line"><span class="section">test:</span></span><br><span class="line"><span class="keyword">ifdef</span> a</span><br><span class="line">	@echo <span class="string">&quot;True&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	@echo <span class="string">&quot;False&quot;</span></span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf *.o main</span><br></pre></td></tr></table></figure>

<p>在终端执行 make test ，会看到输出了以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">True</span><br></pre></td></tr></table></figure>

<h1 id="二、-Makefile-函数"><a href="#二、-Makefile-函数" class="headerlink" title="二、 Makefile 函数"></a><font size=3>二、 Makefile 函数</font></h1><h2 id="1-函数的调用"><a href="#1-函数的调用" class="headerlink" title="1.函数的调用"></a><font size=3>1.函数的调用</font></h2><p>  make 提供了一些函数供 Makefile 调用，函数调用后，函数的返回值可以当做变量来使用。函数的调用与变量很类似，一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(&lt;function&gt; arg1, arg2, ...)</span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line">$&#123;&lt;function&gt; arg1, arg2, ...&#125;</span><br></pre></td></tr></table></figure>

<p> function 就是函数名， argN 是函数的参数。</p>
<p><strong>【注意】</strong></p>
<p>（1）函数名与参数之间通过空格（ space ）分隔开。</p>
<p>（2）函数的多个参数之间使用 , 分割开来，并且在函数中可以使用变量。</p>
<p>（3）为了风格的统一，函数和变量的括号最好一样，如使用 $(subst a,b,$(x)) 这样的形式。</p>
<h2 id="2-字符串处理函数"><a href="#2-字符串处理函数" class="headerlink" title="2.字符串处理函数"></a><font size=3>2.字符串处理函数</font></h2><h3 id="2-1字符串替换函数"><a href="#2-1字符串替换函数" class="headerlink" title="2.1字符串替换函数"></a><font size=3>2.1字符串替换函数</font></h3><h4 id="2-1-1-subst"><a href="#2-1-1-subst" class="headerlink" title="2.1.1 subst "></a><font size=3>2.1.1 subst </font></h4><p>字符串替换函数 subst 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">subst</span> &lt;source_str&gt;,&lt;target_str&gt;,&lt;text&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会将字符串 text 中的 source_str 字符串替换成 target_str 。</p>
<p><strong>【返回值】</strong>函数返回被替换过后的字符串。</p>
<p><strong>【注意】</strong> &lt;source_str&gt;,&lt;target_str&gt;,&lt;text&gt; 的 , 号之间最好不要有空格，否则可能替换后会多个空格哦。</p>
<h4 id="2-1-2使用实例"><a href="#2-1-2使用实例" class="headerlink" title="2.1.2使用实例"></a><font size=3>2.1.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">subst</span> .o,.c,<span class="variable">$(var1)</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o</span><br><span class="line">var2=main.c test.c</span><br></pre></td></tr></table></figure>

<h3 id="2-2模式字符串替换函数"><a href="#2-2模式字符串替换函数" class="headerlink" title="2.2模式字符串替换函数"></a><font size=3>2.2模式字符串替换函数</font></h3><h4 id="2-2-1-patsubst"><a href="#2-2-1-patsubst" class="headerlink" title="2.2.1 patsubst "></a><font size=3>2.2.1 patsubst </font></h4><p>模式字符串替换函数 patsubst  一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">patsubst</span> &lt;pattern&gt;,&lt;replacement&gt;,&lt;text&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会查找 &lt;text&gt; 中的单词（单词以“空格”、“Tab”或“回车”，“换行”分隔）是否符合模式 &lt;pattern&gt; ，如果匹配的话，则以 &lt;replacement&gt; 替换。</p>
<p><strong>【返回值】</strong>函数返回被替换过后的字符串。</p>
<p><strong>【注意】</strong></p>
<p>（1） &lt;pattern&gt;,&lt;replacement&gt;,&lt;text&gt; 的 , 号之间最好不要有空格，否则可能替换后会多个空格哦。</p>
<p>（2） &lt;pattern&gt; 可以包括通配符 % ， 表示任意长度的字符串。如果 &lt;replacement&gt; 中也包含 % ，那么， &lt;replacement&gt; 中的这个 %  将是 &lt;pattern&gt; 中的那个 % 所代表的字串。（另外可以用 \ 来转义一些字符，例如，以 % 来表示真实含义的 % 字符）。</p>
<h4 id="2-2-2使用实例"><a href="#2-2-2使用实例" class="headerlink" title="2.2.2使用实例"></a><font size=3>2.2.2使用实例</font></h4><p>这个函数与变量替换的时候很类似，可以看下边的例子。 Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">patsubst</span> %.o,%.c,<span class="variable">$(var1)</span>)</span></span><br><span class="line">var3 := $(var1:.o=.c)</span><br><span class="line">var4 := $(var1:%.o=%.c)</span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var4=<span class="variable">$(var4)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o</span><br><span class="line">var2=main.c test.c</span><br><span class="line">var3=main.c test.c</span><br><span class="line">var4=main.c test.c</span><br></pre></td></tr></table></figure>

<h3 id="2-3去空格函数"><a href="#2-3去空格函数" class="headerlink" title="2.3去空格函数"></a><font size=3>2.3去空格函数</font></h3><h4 id="2-3-1-strip"><a href="#2-3-1-strip" class="headerlink" title="2.3.1 strip "></a><font size=3>2.3.1 strip </font></h4><p>去空格函数 strip  一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">strip</span> &lt;string&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会去掉字符串 &lt;string&gt; 中开头和结尾的空字符，并且将字符串中的空格合并成为一个空格。</p>
<p><strong>【返回值】</strong>函数返回被去掉空格的字符串值。</p>
<h4 id="2-3-2使用实例"><a href="#2-3-2使用实例" class="headerlink" title="2.3.2使用实例"></a><font size=3>2.3.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := ma  in.o             tes    t.o</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">strip</span> <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=ma  in.o             tes    t.o</span><br><span class="line">var2=ma in.o tes t.o</span><br></pre></td></tr></table></figure>



<h3 id="2-4查找字符串函数"><a href="#2-4查找字符串函数" class="headerlink" title="2.4查找字符串函数"></a><font size=3>2.4查找字符串函数</font></h3><h4 id="2-4-1-findstring"><a href="#2-4-1-findstring" class="headerlink" title="2.4.1 findstring "></a><font size=3>2.4.1 findstring </font></h4><p>查找字符串函数 findstring 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">findstring</span> &lt;find_str&gt;,&lt;string&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会在字符串 &lt;string&gt; 中查找 &lt;find_str&gt; 字符串。</p>
<p><strong>【返回值】</strong>函数如果找到要查找的字符串 &lt;find_str&gt; ，那么返回 &lt;find_str&gt; ，否则返回空字符串。</p>
<p><strong>【注意】</strong></p>
<p>（1） &lt;find_str&gt;,&lt;string&gt; 的 , 号之间最好不要有空格，否则可能会与预期的不太一样。</p>
<h4 id="2-4-2使用实例"><a href="#2-4-2使用实例" class="headerlink" title="2.4.2使用实例"></a><font size=3>2.4.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">findstring</span> main,<span class="variable">$(var1)</span>)</span></span><br><span class="line">var3 := <span class="variable">$(<span class="built_in">findstring</span> hello,<span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o</span><br><span class="line">var2=main</span><br><span class="line">var3=</span><br></pre></td></tr></table></figure>

<h3 id="2-5过滤函数"><a href="#2-5过滤函数" class="headerlink" title="2.5过滤函数"></a><font size=3>2.5过滤函数</font></h3><h4 id="2-5-1-filter"><a href="#2-5-1-filter" class="headerlink" title="2.5.1 filter "></a><font size=3>2.5.1 filter </font></h4><p>过滤函数 filter 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">filter</span> &lt;pattern...&gt;,&lt;text&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会过滤出  &lt;text&gt; 中符合模式  &lt;pattern&gt;  的字符串，可以有多个  &lt;pattern&gt; 。</p>
<p><strong>【返回值】</strong>函数返回符合模式 &lt;pattern&gt; 的字符串。</p>
<p><strong>【注意】</strong></p>
<p>（1） &lt;pattern…&gt;,&lt;text&gt; 的 , 号之间最好不要有空格，否则可能会与预期的不太一样。</p>
<h4 id="2-5-2使用实例"><a href="#2-5-2使用实例" class="headerlink" title="2.5.2使用实例"></a><font size=3>2.5.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o main.c test.s test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">filter</span> %.c %.s,<span class="variable">$(var1)</span>)</span></span><br><span class="line">var3 := <span class="variable">$(<span class="built_in">filter</span> %.o,<span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o main.c test.s test.c</span><br><span class="line">var2=main.c test.s test.c</span><br><span class="line">var3=main.o test.o</span><br></pre></td></tr></table></figure>

<h3 id="2-6反过滤函数"><a href="#2-6反过滤函数" class="headerlink" title="2.6反过滤函数"></a><font size=3>2.6反过滤函数</font></h3><h4 id="2-6-1-filter-out"><a href="#2-6-1-filter-out" class="headerlink" title="2.6.1 filter-out "></a><font size=3>2.6.1 filter-out </font></h4><p>反过滤函数 filter-out 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">filter</span>-out &lt;pattern...&gt;,&lt;text&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会过滤出  &lt;text&gt; 中不符合模式  &lt;pattern&gt;  的字符串（就是会把符合的给去掉），可以有多个  &lt;pattern&gt; 。</p>
<p><strong>【返回值】</strong>函数返回不符合模式 &lt;pattern&gt; 的字符串。</p>
<p><strong>【注意】</strong></p>
<p>（1） &lt;pattern…&gt;,&lt;text&gt; 的 , 号之间最好不要有空格，否则可能会与预期的不太一样。</p>
<h4 id="2-6-2使用实例"><a href="#2-6-2使用实例" class="headerlink" title="2.6.2使用实例"></a><font size=3>2.6.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o main.c test.s test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">filter</span>-out %.c %.s,<span class="variable">$(var1)</span>)</span></span><br><span class="line">var3 := <span class="variable">$(<span class="built_in">filter</span>-out %.o,<span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o main.c test.s test.c</span><br><span class="line">var2=main.o test.o</span><br><span class="line">var3=main.c test.s test.c</span><br></pre></td></tr></table></figure>

<h3 id="2-7排序函数"><a href="#2-7排序函数" class="headerlink" title="2.7排序函数"></a><font size=3>2.7排序函数</font></h3><h4 id="2-7-1-sort"><a href="#2-7-1-sort" class="headerlink" title="2.7.1 sort "></a><font size=3>2.7.1 sort </font></h4><p>排序函数 sort 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">sort</span> &lt;list&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数给字符串 &lt;list&gt; 中的单词排序（升序）。</p>
<p><strong>【返回值】</strong>函数返回排序后的字符串。</p>
<h4 id="2-7-2使用实例"><a href="#2-7-2使用实例" class="headerlink" title="2.7.2使用实例"></a><font size=3>2.7.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := a.o b.c d.s a.c x.c </span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">sort</span> <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=a.o b.c d.s a.c x.c </span><br><span class="line">var2=a.c a.o b.c d.s x.c</span><br></pre></td></tr></table></figure>



<h3 id="2-8取单词函数"><a href="#2-8取单词函数" class="headerlink" title="2.8取单词函数"></a><font size=3>2.8取单词函数</font></h3><h4 id="2-8-1-word"><a href="#2-8-1-word" class="headerlink" title="2.8.1 word "></a><font size=3>2.8.1 word </font></h4><p>取单词函数 word 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">word</span> &lt;n&gt;,&lt;text&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会取字符串 &lt;text&gt; 中第 n 个单词。（ n 从 1 开始）。</p>
<p><strong>【返回值】</strong>函数返回字符串 &lt;text&gt; 中第 n 个单词。如果 n 比 &lt;text&gt; 中的单词数要大，那么返回空字符串。</p>
<h4 id="2-8-2使用实例"><a href="#2-8-2使用实例" class="headerlink" title="2.8.2使用实例"></a><font size=3>2.8.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o main.c test.s test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">word</span> 1, <span class="variable">$(var1)</span>)</span></span><br><span class="line">var3 := <span class="variable">$(<span class="built_in">word</span> 3, <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o main.c test.s test.c</span><br><span class="line">var2=main.o</span><br><span class="line">var3=main.c</span><br></pre></td></tr></table></figure>



<h3 id="2-9单词个数统计函数"><a href="#2-9单词个数统计函数" class="headerlink" title="2.9单词个数统计函数"></a><font size=3>2.9单词个数统计函数</font></h3><h4 id="2-9-1-words"><a href="#2-9-1-words" class="headerlink" title="2.9.1 words "></a><font size=3>2.9.1 words </font></h4><p>单词个数统计函数 words 一般语法格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$</span><span class="language-bash">(words &lt;text&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会统计字符串 &lt;text&gt; 中的单词个数。</p>
<p><strong>【返回值】</strong>函数返回字符串 &lt;text&gt; 中的单词数。</p>
<h4 id="2-9-2使用实例"><a href="#2-9-2使用实例" class="headerlink" title="2.9.2使用实例"></a><font size=3>2.9.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o main.c test.s test.c</span><br><span class="line">var2 := <span class="variable">$(words <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o main.c test.s test.c</span><br><span class="line">var2=5</span><br></pre></td></tr></table></figure>



<h3 id="2-10首单词函数"><a href="#2-10首单词函数" class="headerlink" title="2.10首单词函数"></a><font size=3>2.10首单词函数</font></h3><h4 id="2-10-1-firstword"><a href="#2-10-1-firstword" class="headerlink" title="2.10.1 firstword "></a><font size=3>2.10.1 firstword </font></h4><p>首单词函数 firstword 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">firstword</span> &lt;text&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会取出字符串 &lt;text&gt; 中的第一个单词。</p>
<p><strong>【返回值】</strong>函数返回字符串 &lt;text&gt; 中的第一个单词。</p>
<h4 id="2-10-2使用实例"><a href="#2-10-2使用实例" class="headerlink" title="2.10.2使用实例"></a><font size=3>2.10.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.o test.o main.c test.s test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">firstword</span> <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.o test.o main.c test.s test.c</span><br><span class="line">var2=main.o</span><br></pre></td></tr></table></figure>



<h3 id="2-11综合应用"><a href="#2-11综合应用" class="headerlink" title="2.11综合应用"></a><font size=3>2.11综合应用</font></h3><ul>
<li>指定编译器头文件搜索路径</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CFLAGS += = <span class="variable">$(<span class="built_in">patsubst</span> %,-I %,$(<span class="built_in">subst</span> :, ,<span class="variable">$(VPATH)</span>)</span>)</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看变量赋值分析 </summary>
              <div class='content'>
              <p>我们先假设 VPATH:&#x3D;.&#x2F;src : .&#x2F;include ，那么：</p><table>    <tr><td align="center" width=200px>CFLAGS</td><td align="left">是代表C编译器的选项</td></tr>    <tr><td align="center" width=200px>+=</td><td align="left">是代表追加赋值</td></tr>    <tr><td align="center" width=200px>$(VPATH)</td><td align="left">文件搜索路径，在这里表示 ./src : ./include</td></tr>    <tr><td align="center" width=200px>$(subst :, ,$(VPATH))</td><td align="left">调用 subst 字符串替换函数，将 ./src : ./include 中的 : 替换为空值（将会产生一个空格），最终得到字符串<br> ./src   ./include</td></tr>    <tr><td align="center" width=200px>patsubst</td><td align="left">模式字符串替换函数，将会把 ./src   ./include 的每个单词进行整体匹配，会得到<br> -I ./src -I ./include</td></tr></table><p>所以最终我们会得到 cc  或  gcc  搜索头文件路径的参数。放入 Makefile 文件，</p><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">VPATH := ./src : ./<span class="keyword">include</span></span><br><span class="line">var1 := <span class="variable">$(VPATH)</span></span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">subst</span> :, ,<span class="variable">$(VPATH)</span>)</span></span><br><span class="line">CFLAGS += <span class="variable">$(<span class="built_in">patsubst</span> %,-I %,$(<span class="built_in">subst</span> :, ,<span class="variable">$(VPATH)</span>)</span>)</span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;CFLAGS=<span class="variable">$(CFLAGS)</span>&quot;</span></span><br></pre></td></tr></table></figure><p>接着执行 make test ，会看到输出如下：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var1=./src : ./include</span><br><span class="line">var2=./src   ./include</span><br><span class="line">CFLAGS=-I ./src -I ./include</span><br></pre></td></tr></table></figure><p>若修改为以下内容，限制会更强一点：</p><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CFLAGS += <span class="variable">$(<span class="built_in">patsubst</span> %,-I %,$(<span class="built_in">filter</span> %include,$(<span class="built_in">subst</span> :, ,<span class="variable">$(VPATH)</span>)</span>))</span><br></pre></td></tr></table></figure><p>这样最终就只剩下 -I .&#x2F;include 了。</p>
              </div>
            </details>

<h2 id="3-文件名操作函数"><a href="#3-文件名操作函数" class="headerlink" title="3.文件名操作函数"></a><font size=3>3.文件名操作函数</font></h2><h3 id="3-1取目录函数"><a href="#3-1取目录函数" class="headerlink" title="3.1取目录函数"></a><font size=3>3.1取目录函数</font></h3><h4 id="3-1-1-dir"><a href="#3-1-1-dir" class="headerlink" title="3.1.1 dir "></a><font size=3>3.1.1 dir </font></h4><p>取目录函数 dir 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">dir</span> &lt;names...&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会从文件名序列 &lt;names…&gt; 中取出目录部分。目录部分是指最后一个反斜杠（ &#x2F; ）之前的部分。如果没有反斜杠，那么返回 .&#x2F; 。</p>
<p><strong>【返回值】</strong>函数返回文件名序列的目录部分。</p>
<h4 id="3-1-2使用实例"><a href="#3-1-2使用实例" class="headerlink" title="3.1.2使用实例"></a><font size=3>3.1.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.c ./<span class="keyword">include</span>/test.h ./src/test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">dir</span> <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c ./include/test.h ./src/test.c</span><br><span class="line">var2=./ ./include/ ./src/</span><br></pre></td></tr></table></figure>



<h3 id="3-2取文件函数"><a href="#3-2取文件函数" class="headerlink" title="3.2取文件函数"></a><font size=3>3.2取文件函数</font></h3><h4 id="3-2-1-notdir"><a href="#3-2-1-notdir" class="headerlink" title="3.2.1 notdir "></a><font size=3>3.2.1 notdir </font></h4><p>取文件函数 notdir 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">notdir</span> &lt;names...&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会从文件名序列 &lt;names…&gt; 中取出非目录部分。非目录部分是指最后一个反斜杠（ &#x2F; ）之后的部分。</p>
<p><strong>【返回值】</strong>函数返回文件名序列的目录部分。</p>
<h4 id="3-2-2使用实例"><a href="#3-2-2使用实例" class="headerlink" title="3.2.2使用实例"></a><font size=3>3.2.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.c ./<span class="keyword">include</span>/test.h ./src/test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">notdir</span> <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c ./include/test.h ./src/test.c</span><br><span class="line">var2=main.c test.h test.c</span><br></pre></td></tr></table></figure>

<h3 id="3-3取后缀函数"><a href="#3-3取后缀函数" class="headerlink" title="3.3取后缀函数"></a><font size=3>3.3取后缀函数</font></h3><h4 id="3-3-1-suffix"><a href="#3-3-1-suffix" class="headerlink" title="3.3.1 suffix "></a><font size=3>3.3.1 suffix </font></h4><p>取后缀函数 suffix 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">suffix</span> &lt;names...&gt;)</span> </span><br></pre></td></tr></table></figure>

<p>【函数说明】该函数会从文件名序列 &lt;names…&gt; 中取出各个文件的后缀。</p>
<p>【返回值】函数返回文件名序列 &lt;names …&gt; 的后缀序列，如果文件没有后缀，则返回空字串。</p>
<h4 id="3-3-2使用实例"><a href="#3-3-2使用实例" class="headerlink" title="3.3.2使用实例"></a><font size=3>3.3.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.c ./<span class="keyword">include</span>/test.h ./src/test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">suffix</span> <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c ./include/test.h ./src/test.c</span><br><span class="line">var2=.c .h .c</span><br></pre></td></tr></table></figure>



<h3 id="3-4取前缀函数"><a href="#3-4取前缀函数" class="headerlink" title="3.4取前缀函数"></a><font size=3>3.4取前缀函数</font></h3><h4 id="3-4-1-basename"><a href="#3-4-1-basename" class="headerlink" title="3.4.1 basename "></a><font size=3>3.4.1 basename </font></h4><p>取前缀函数 basename 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">basename</span> &lt;names...&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会从文件名序列 &lt;names…&gt; 中取出各个文件的前缀部分。</p>
<p><strong>【返回值】</strong>函数返回文件名序列 &lt;names …&gt; 的前缀序列，如果文件没有前缀，则返回空字串。</p>
<p><strong>【注意】</strong>取出的前缀会包含目录部分（如果有目录部分的话）。</p>
<h4 id="3-4-2使用实例"><a href="#3-4-2使用实例" class="headerlink" title="3.4.2使用实例"></a><font size=3>3.4.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.c ./<span class="keyword">include</span>/test.h ./src/test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">basename</span> <span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c ./include/test.h ./src/test.c</span><br><span class="line">var2=main ./include/test ./src/test</span><br></pre></td></tr></table></figure>



<h3 id="3-5加后缀函数"><a href="#3-5加后缀函数" class="headerlink" title="3.5加后缀函数"></a><font size=3>3.5加后缀函数</font></h3><h4 id="3-5-1-addsuffix"><a href="#3-5-1-addsuffix" class="headerlink" title="3.5.1 addsuffix "></a><font size=3>3.5.1 addsuffix </font></h4><p>加后缀函数 addsuffix 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">addsuffix</span> &lt;<span class="built_in">suffix</span>&gt;,&lt;names...&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会把后缀 &lt;suffix&gt; 加到 &lt;names…&gt; 中的每个单词后面。</p>
<p><strong>【返回值】</strong>函数返回加过后缀的文件名序列。</p>
<h4 id="3-5-2使用实例"><a href="#3-5-2使用实例" class="headerlink" title="3.5.2使用实例"></a><font size=3>3.5.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.c ./<span class="keyword">include</span>/test.h ./src/test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">addsuffix</span> -QN,<span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c ./include/test.h ./src/test.c</span><br><span class="line">var2=main.c-QN ./include/test.h-QN ./src/test.c-QN</span><br></pre></td></tr></table></figure>

<h3 id="3-6加前缀函数"><a href="#3-6加前缀函数" class="headerlink" title="3.6加前缀函数"></a><font size=3>3.6加前缀函数</font></h3><h4 id="3-6-1-addprefix"><a href="#3-6-1-addprefix" class="headerlink" title="3.6.1 addprefix "></a><font size=3>3.6.1 addprefix </font></h4><p>加前缀函数 addprefix 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">addprefix</span> &lt;prefix&gt;,&lt;names...&gt;)</span> </span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会把前缀 &lt;prefix&gt; 加到 &lt;names…&gt; 中的每个单词前面。</p>
<p><strong>【返回值】</strong>函数返回加过前缀的文件名序列。</p>
<h4 id="3-6-2使用实例"><a href="#3-6-2使用实例" class="headerlink" title="3.6.2使用实例"></a><font size=3>3.6.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.c <span class="keyword">include</span>/test.h src/test.c</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">addprefix</span> ./,<span class="variable">$(var1)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c include/test.h src/test.c</span><br><span class="line">var2=./main.c ./include/test.h ./src/test.c</span><br></pre></td></tr></table></figure>

<h3 id="3-7连接函数"><a href="#3-7连接函数" class="headerlink" title="3.7连接函数"></a><font size=3>3.7连接函数</font></h3><h4 id="3-7-1-join"><a href="#3-7-1-join" class="headerlink" title="3.7.1 join "></a><font size=3>3.7.1 join </font></h4><p>连接函数 join 一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">join</span> &lt;list1&gt;,&lt;list2&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会把 &lt;list2&gt; 中的单词<strong>对应地</strong>加到 &lt;list1&gt; 的单词后面。</p>
<p><strong>【返回值】</strong>函数返回连接过后的字符串。</p>
<p><strong>【注意】</strong>如果 &lt;list1&gt; 的单词个数要比 &lt;list2&gt; 的多，那么， &lt;list1&gt; 中的多出来的单词将保持原样。如果 &lt;list1&gt; 的单词个数要比 &lt;list2&gt; 少，那么，  &lt;list2&gt; 多出来的单词将被复制到 &lt;list1&gt; 中。</p>
<h4 id="3-7-2使用实例"><a href="#3-7-2使用实例" class="headerlink" title="3.7.2使用实例"></a><font size=3>3.7.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">var1 := 111 112 </span><br><span class="line">var2 := -221 -222 -223</span><br><span class="line">var3 := 331</span><br><span class="line">var4 := <span class="variable">$(<span class="built_in">join</span> <span class="variable">$(var1)</span>,<span class="variable">$(var2)</span>)</span></span><br><span class="line">var5 := <span class="variable">$(<span class="built_in">join</span> <span class="variable">$(var3)</span>,<span class="variable">$(var2)</span>)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var4=<span class="variable">$(var4)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var5=<span class="variable">$(var5)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1=111 112 </span><br><span class="line">var2=-221 -222 -223</span><br><span class="line">var3=331</span><br><span class="line">var4=111-221 112-222 -223</span><br><span class="line">var5=331-221 -222 -223</span><br></pre></td></tr></table></figure>

<h3 id="3-8获取匹配模式文件名函数"><a href="#3-8获取匹配模式文件名函数" class="headerlink" title="3.8获取匹配模式文件名函数"></a><font size=3>3.8获取匹配模式文件名函数</font></h3><h4 id="3-8-1-wildcard"><a href="#3-8-1-wildcard" class="headerlink" title="3.8.1 wildcard "></a><font size=3>3.8.1 wildcard </font></h4><p>获取匹配模式文件名函数 wildcard  一般语法格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">wildcard</span>  &lt;pattern&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数会列出<strong>当前目录</strong>下所有符合模式的  &lt;pattern&gt;  格式的文件名，可以有多个 &lt;pattern&gt; ，中间用空格隔开。</p>
<p><strong>【返回值】</strong>函数返回返回值为<strong>空格分隔</strong>并且存在当前目录下的所有符合模式  &lt;pattern&gt;  的文件名。</p>
<p><strong>【注意】</strong></p>
<p>（1）在这个函数中似乎不太适合使用 % 来匹配，可以使用其他的通配符来进行匹配。</p>
<h4 id="3-8-2使用实例"><a href="#3-8-2使用实例" class="headerlink" title="3.8.2使用实例"></a><font size=3>3.8.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var1 := <span class="variable">$(<span class="built_in">wildcard</span> *.c *.o)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c test.c main.o test.o</span><br></pre></td></tr></table></figure>

<p>便会列出当前目录下所有的 .c 和 .o 文件。</p>
<h2 id="4-其它常用函数"><a href="#4-其它常用函数" class="headerlink" title="4.其它常用函数"></a><font size=3>4.其它常用函数</font></h2><h3 id="4-1-foreach-函数"><a href="#4-1-foreach-函数" class="headerlink" title="4.1 foreach  函数"></a><font size=3>4.1 foreach  函数</font></h3><h4 id="4-1-1-foreach"><a href="#4-1-1-foreach" class="headerlink" title="4.1.1 foreach "></a><font size=3>4.1.1 foreach </font></h4><p>循环函数 foreach  一般使用格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">foreach</span> &lt;var&gt;,&lt;list&gt;,&lt;text&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>把参数 &lt;list&gt; 中的单词逐一取出放到参数 &lt;var&gt; 所指定的变量中，然后再执行 &lt;text&gt; 所包含的表达式。每一次 &lt;text&gt; 会返回一个字符串。</p>
<p>循环过程中， &lt;text&gt; 的返所返回的每个字符串会以<strong>空格分割</strong>，最后当整个循环结束的时候， &lt;text&gt; 所返回的每个字符串所组成的整个字符串（以空格分隔）将会是  foreach  函数的返回值。</p>
<p>所以 &lt;var&gt; 最好是一个变量名， &lt;list&gt; 可以是一个表达式，而 &lt;text&gt; 中一般会只用 &lt;var&gt; 这个参数来一次枚举 &lt;list&gt; 中的单词。</p>
<p><strong>【注意】</strong> foreach  中的 &lt;var&gt;  参数是一个临时的局部变量， foreach  函数执行完后，参数 &lt;var&gt; 的变量将可用，其作用域只在  foreach  函数当中。</p>
<h4 id="4-1-1使用实例"><a href="#4-1-1使用实例" class="headerlink" title="4.1.1使用实例"></a><font size=3>4.1.1使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var1 := a b c d</span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">foreach</span> n,<span class="variable">$(var1)</span>,./<span class="variable">$(n)</span>.o)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var1=a b c d</span><br><span class="line">var2=./a.o ./b.o ./c.o ./d.o</span><br></pre></td></tr></table></figure>

<p> $(var1) 中的单词会被挨个取出，并存到变量 n 中， $(n).o 每次根据 $(n) 计算出一个值，这些值以空格分隔，最后作为  foreach  函数的返回值。</p>
<h3 id="4-2-if-函数"><a href="#4-2-if-函数" class="headerlink" title="4.2 if 函数"></a><font size=3>4.2 if 函数</font></h3><h4 id="4-2-1-if"><a href="#4-2-1-if" class="headerlink" title="4.2.1 if "></a><font size=3>4.2.1 if </font></h4><p>条件函数 if  一般使用格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">if</span> &lt;condition&gt;,&lt;then-part&gt;)</span> </span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line"><span class="variable">$(<span class="built_in">if</span> &lt;condition&gt;,&lt;then-part&gt;,&lt;else-part&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> &lt;condition&gt; 参数是  if  的表达式，如果其返回的是非空的字符串，那么这个表达式就相当于返回真，于是， &lt;then-part&gt; 就会被计算，否则 &lt;else-part&gt; 会被计算。</p>
<p><strong>【返回值】</strong></p>
<p>如果 &lt;condition&gt; 为真（非空字符串），那么 &lt;then-part&gt; 会是整个函数的返回值。</p>
<p>如果 &lt;condition&gt; 为假（空字符串），那么 &lt;else-part&gt; 将会是这个函数的返回值。此时如果 &lt;else-part&gt; 没有被定义（也就是说没有这一部分），那么整个函数返回空字串符。所以， &lt;then-part&gt; 和 &lt;else-part&gt; 只会有一个被计算。</p>
<h4 id="4-2-2使用实例"><a href="#4-2-2使用实例" class="headerlink" title="4.2.2使用实例"></a><font size=3>4.2.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">var1 := a b c d</span><br><span class="line">var2 := </span><br><span class="line">var3 := <span class="variable">$(<span class="built_in">if</span> <span class="variable">$(var1)</span>,<span class="variable">$(var1)</span>,false)</span></span><br><span class="line">var4 := <span class="variable">$(<span class="built_in">if</span> <span class="variable">$(var2)</span>,<span class="variable">$(var2)</span>)</span></span><br><span class="line">var4 := <span class="variable">$(<span class="built_in">if</span> <span class="variable">$(var2)</span>,<span class="variable">$(var2)</span>,false)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var4=<span class="variable">$(var4)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var1=a b c d</span><br><span class="line">var2=</span><br><span class="line">var3=a b c d</span><br><span class="line">var4=false</span><br></pre></td></tr></table></figure>

<h3 id="4-3-call-函数"><a href="#4-3-call-函数" class="headerlink" title="4.3 call 函数"></a><font size=3>4.3 call 函数</font></h3><h4 id="4-3-1-call"><a href="#4-3-1-call" class="headerlink" title="4.3.1 call "></a><font size=3>4.3.1 call </font></h4><p>传递参数函数 call 一般使用格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">call</span> &lt;expression&gt;,&lt;parm1&gt;,&lt;parm2&gt;...)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> &lt;expression&gt; 参数是  call  的表达式，我们可以在表达式 &lt;expression&gt; 中定义许多的参数，然后通过 call 函数向表达式传入 &lt;parm1&gt;,&lt;parm2&gt;… 参数。</p>
<p><strong>【返回值】</strong> &lt;expression&gt; 的返回值就是函数的返回值。</p>
<p><strong>【注意】</strong></p>
<p>（1） &lt;expression&gt; 可以定义为以下格式：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var1 = $(1) $(2) $(3)</span><br></pre></td></tr></table></figure>

<p> $(1) $(2) $(3) 分别表示第一个参数，第二个参数，第三个参数，参数的次序是可以自定义的，不一定是顺序的。另外说明一点就是，这里定义的时候不能用 :&#x3D; 否则无法对传入变量进行引用。</p>
<p>（2）传入参数多余或者少于表达式中定义的参数，都不会报错，若引用参数的编号超出了传入参数数量，会多出一个空格，下边例子中就有。</p>
<h4 id="4-3-2使用实例"><a href="#4-3-2使用实例" class="headerlink" title="4.3.2使用实例"></a><font size=3>4.3.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">var1 = $(1) $(2) $(3)</span><br><span class="line">var2 = $(3) $(1) $(2)</span><br><span class="line">var3 :=  <span class="variable">$(<span class="built_in">call</span> var1,a,b,c)</span></span><br><span class="line">var4 :=  <span class="variable">$(<span class="built_in">call</span> var2,a,b,c)</span></span><br><span class="line">var5 :=  <span class="variable">$(<span class="built_in">call</span> var1,a,b)</span></span><br><span class="line">var6 :=  <span class="variable">$(<span class="built_in">call</span> var2,a,b)</span></span><br><span class="line">var7 :=  <span class="variable">$(<span class="built_in">call</span> var1,a,b,c,d)</span></span><br><span class="line">var8 :=  <span class="variable">$(<span class="built_in">call</span> var2,a,b,c,d)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var4=<span class="variable">$(var4)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var5=<span class="variable">$(var5)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var6=<span class="variable">$(var6)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var7=<span class="variable">$(var7)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var8=<span class="variable">$(var8)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var1=  </span><br><span class="line">var2=  </span><br><span class="line">var3=a b c</span><br><span class="line">var4=c a b</span><br><span class="line">var5=a b </span><br><span class="line">var6= a b</span><br><span class="line">var7=a b c</span><br><span class="line">var8=c a b</span><br></pre></td></tr></table></figure>

<h3 id="4-4-origin-函数"><a href="#4-4-origin-函数" class="headerlink" title="4.4 origin 函数"></a><font size=3>4.4 origin 函数</font></h3><h4 id="4-4-1-origin"><a href="#4-4-1-origin" class="headerlink" title="4.4.1 origin "></a><font size=3>4.4.1 origin </font></h4><p>溯源函数（哈哈🤣，我自己起的名字，感觉叫着方便） origin 一般使用格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">origin</span> &lt;variable&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>不操作变量的值，只是告诉我们 &lt;variable&gt; 这个变量是哪里来的。</p>
<p><strong>【返回值】</strong>这个函数的返回值就多了：</p>
<table>
    <tr><td align="center" width=100px>undefined</td><td align="left">variable从来没有定义过，origin 函数返回这个值“undefined”。</td></tr>
    <tr><td align="center" width=100px>default</td><td align="left">variable是一个默认的定义，比如“CC”这个变量，</td></tr>
    <tr><td align="center" width=100px>environment</td><td align="left">variable是一个环境变量，并且当 Makefile 被执行时，“-e”参数没有被打开。</td></tr>
    <tr><td align="center" width=100px>file</td><td align="left">variable这个变量被定义在 Makefile 中。</td></tr>
    <tr><td align="center" width=100px>command line</td><td align="left">variable这个变量是被命令行定义的。</td></tr>
    <tr><td align="center" width=100px>override</td><td align="left">variable是被 override 指示符重新定义的。</td></tr>
    <tr><td align="center" width=100px>automatic</td><td align="left">variable是一个命令运行中的自动化变量。</td></tr>
</table>



<h4 id="4-4-2使用实例"><a href="#4-4-2使用实例" class="headerlink" title="4.4.2使用实例"></a><font size=3>4.4.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var1 := main.c</span><br><span class="line">var2 :=  <span class="variable">$(<span class="built_in">origin</span> var1)</span></span><br><span class="line">var3 :=  <span class="variable">$(<span class="built_in">origin</span> CC)</span></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var1=main.c</span><br><span class="line">var2=file</span><br><span class="line">var3=default</span><br></pre></td></tr></table></figure>

<h3 id="4-5-shell-函数"><a href="#4-5-shell-函数" class="headerlink" title="4.5 shell 函数"></a><font size=3>4.5 shell 函数</font></h3><h4 id="4-5-1-shell"><a href="#4-5-1-shell" class="headerlink" title="4.5.1 shell "></a><font size=3>4.5.1 shell </font></h4><p> shell 函数一般使用格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">shell</span> &lt;command&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong> &lt;command&gt; 为函数的参数，可以是 shell 中的命令。这个函数就是可以调用 shell 命令，它与反引号  ` 功能相同。</p>
<p><strong>【返回值】</strong> shell 命令运行结果。</p>
<p><strong>【注意】</strong>这个函数会新生成一个  Shell  程序来执行命令，所以我们需要注意其运行性能，如果我们的  Makefile  中有一些比较复杂的规则，并大量使用了这个函数，那么对于我们的系统性能是有害的。特别是  Makefile  的隐晦的规则可能会让 shell 函数执行的次数比想像的多得多。</p>
<h4 id="4-5-2使用实例"><a href="#4-5-2使用实例" class="headerlink" title="4.5.2使用实例"></a><font size=3>4.5.2使用实例</font></h4><p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var1 := <span class="variable">$(<span class="built_in">shell</span> pwd)</span></span><br><span class="line">var2 := <span class="variable">$(<span class="built_in">shell</span> ls)</span></span><br><span class="line">var3 :=  pwd </span><br><span class="line">var4 :=  ls </span><br><span class="line"><span class="section">test:</span></span><br><span class="line">	@echo <span class="string">&quot;var1=<span class="variable">$(var1)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var2=<span class="variable">$(var2)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var3=<span class="variable">$(var3)</span>&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;var4=<span class="variable">$(var4)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">var1=/mnt/hgfs/sharedfiles/2Linux/test/LV02/05Makefile/test1</span><br><span class="line">var2=main main.c main.o Makefile test.c test.h test.o</span><br><span class="line">var3=/home/hk/0sharedfiles/2Linux/test/LV02/05Makefile/test1</span><br><span class="line">var4=main</span><br><span class="line">main.c</span><br><span class="line">main.o</span><br><span class="line">Makefile</span><br><span class="line">test.c</span><br><span class="line">test.h</span><br><span class="line">test.o</span><br></pre></td></tr></table></figure>

<h1 id="三、-make-控制函数"><a href="#三、-make-控制函数" class="headerlink" title="三、 make 控制函数"></a><font size=3>三、 make 控制函数</font></h1><p> Makefile  中提供了两个控制  make  运行方式的函数。其作用是当  make  执行过程中检测到某些错误时为用户提供消息，并且可以控制  make  执行过程是否继续。</p>
<h2 id="1-error-函数"><a href="#1-error-函数" class="headerlink" title="1. error 函数"></a><font size=3>1. error 函数</font></h2><h3 id="1-1-error"><a href="#1-1-error" class="headerlink" title="1.1 error "></a><font size=3>1.1 error </font></h3><p>一般语法格式：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">error</span> &lt;text ...&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>产生致命错误，并提示  &lt;text …&gt;  信息给用户，并退出  make  的执行。</p>
<p> error  函数是在函数展开时（函数被调用时）才提示信息并结束  make  进程。因此如果函数出现在命令中或者一个递归的变量定义时，读取  Makefile  时不会出现错误。而只有包含  error  函数引用的命令被执行，或者定义中引用此函数的递归变量被展开时，才会提示致命信息   &lt;text …&gt; 同时退出  make 。</p>
<p><strong>【返回值】</strong>无</p>
<p><strong>【注意】</strong> error 函数一般不出现在直接展开式的变量定义中，否则在  make  读取  Makefile  时将会提示致命错误。</p>
<h3 id="1-2使用实例"><a href="#1-2使用实例" class="headerlink" title="1.2使用实例"></a><font size=3>1.2使用实例</font></h3><ul>
<li>读取时直接报错</li>
</ul>
<p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ERROR := E1</span><br><span class="line"><span class="section">test:</span></span><br><span class="line">    <span class="keyword">ifdef</span> ERROR</span><br><span class="line">    <span class="variable">$(<span class="built_in">error</span> <span class="built_in">error</span> is <span class="variable">$(ERROR)</span>)</span></span><br><span class="line">    <span class="keyword">endif</span> </span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make test 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Makefile:4: *** error is E1。 停止。</span><br></pre></td></tr></table></figure>

<ul>
<li>执行时报错</li>
</ul>
<p> Makefile 文件内容如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ERROR=<span class="variable">$(<span class="built_in">error</span> found an <span class="built_in">error</span>!)</span></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>:error</span></span><br><span class="line"><span class="section">error:</span></span><br><span class="line">	<span class="variable">$(ERROR)</span></span><br></pre></td></tr></table></figure>

<p>然后在终端中执行 make error 命令，可以看到输出如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Makefile:4: *** found an error!。 停止。</span><br></pre></td></tr></table></figure>

<p>在  make  读取  Makefile  时不会出现致命错误，只有目标  error 被作为是一个目标被执行时才会出现。</p>
<h2 id="2-warning-函数"><a href="#2-warning-函数" class="headerlink" title="2. warning 函数"></a><font size=3>2. warning 函数</font></h2><h3 id="2-1-warning"><a href="#2-1-warning" class="headerlink" title="2.1 warning "></a><font size=3>2.1 warning </font></h3><p>一般语法格式：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">warning</span> &lt;text ...&gt;)</span></span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>产生警告信息 &lt;text …&gt;  ，  make  的执行过程会继续。</p>
<p><strong>【返回值】</strong>无</p>
<p><strong>【注意】</strong>用法和  error  类似，展开过程相同。</p>
<h1 id="四、-Makefile-中的-for-循环"><a href="#四、-Makefile-中的-for-循环" class="headerlink" title="四、 Makefile 中的 for 循环"></a><font size=3>四、 Makefile 中的 for 循环</font></h1><h2 id="1-for-格式"><a href="#1-for-格式" class="headerlink" title="1. for 格式"></a><font size=3>1. for 格式</font></h2><p>在 Makefile 中也是可以使用 for 循环的，格式如下：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">for var in <span class="variable">$(LIST)</span>;do command;done</span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line">for var in <span class="variable">$(LIST)</span>;\</span><br><span class="line">	do \</span><br><span class="line">		command; \</span><br><span class="line">	done</span><br></pre></td></tr></table></figure>

<p><strong>【注意】</strong>使用 var 的时候，一定要是 $$var ，且不能用 () 或者 { } 将 var 包裹。</p>
<h2 id="2-使用实例"><a href="#2-使用实例" class="headerlink" title="2.使用实例"></a><font size=3>2.使用实例</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">x = +O</span><br><span class="line"><span class="section">main:</span></span><br><span class="line">	@for var in a b c d; \</span><br><span class="line">	do \</span><br><span class="line">		echo $$var <span class="variable">$(x)</span> $$var.o;\</span><br><span class="line">	done</span><br></pre></td></tr></table></figure>

<p>在终端执行 make main 会有如下输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a +O a.o</span><br><span class="line">b +O b.o</span><br><span class="line">c +O c.o</span><br><span class="line">d +O d.o</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/8aa811b0.html">LV02-05-Makefile-07-条件判断和函数</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年03月23日 - 13:58</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/8aa811b0.html" title="LV02-05-Makefile-07-条件判断和函数">https://sumumm.github.io/post/8aa811b0.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV02-编译基础</a>
              <a href="/tags/makefile/" rel="tag"><i class="fa fa-tag"></i> makefile</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/f4012629.html" rel="prev" title="LV03-01-数据结构-数据结构基础">
                  <i class="fa fa-angle-left"></i> LV03-01-数据结构-数据结构基础
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/35083dd3.html" rel="next" title="LV02-05-Makefile-06-文件包含和嵌套执行">
                  LV02-05-Makefile-06-文件包含和嵌套执行 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
